/*
 * Created on Mar 7, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.tag;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.hp.hpl.jena.rdf.model.Resource;

import edu.mit.simile.longwell.ui.FlairMessage;
import edu.mit.simile.longwell.ui.command.IFlairCommand;
import edu.mit.simile.longwell.Profile;
import edu.mit.simile.longwell.query.IQueryModel;
import edu.mit.simile.longwell.query.Query;

/**
 * @author dfhuynh
 */
public class TagCommand implements IFlairCommand {
	final static private Logger s_logger =
		Logger.getLogger(TagCommand.class);

	/* (non-Javadoc)
	 * @see edu.mit.simile.flair.command.IFlairCommand#execute(edu.mit.simile.flair.FlairMessage)
	 */
	public void execute(FlairMessage msg) throws ServletException {
		try {
			BufferedReader br = new BufferedReader(
				new InputStreamReader(msg.m_request.getInputStream()));

			String subCommand = br.readLine();
			if ("complete".equals(subCommand)) {
				String prefix = br.readLine();
				doComplete(msg, prefix);
			} else if ("tag".equals(subCommand)) {
				String pseudoURI = br.readLine();
				String tags = br.readLine();

				doTag(msg, pseudoURI, tags);
			} else if ("tagAll".equals(subCommand)) {
				String tags = br.readLine();

				doTagAll(msg, tags);
			}
		} catch (Exception e) {
			s_logger.error(e);
			e.printStackTrace();
			msg.m_response.setStatus(
				HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
	}

	protected void doComplete(FlairMessage msg, String prefix) throws Exception {
		ITagModel tagModel = (ITagModel)
			msg.getProfile().getStructuredModel(ITagModel.class);

		List completions = tagModel.completeTags(prefix, 10);

		OutputStream os = msg.m_response.getOutputStream();
		PrintStream ps = new PrintStream(os);

		ps.print(prefix + "\n");

		Iterator i = completions.iterator();
		while (i.hasNext()) {
			String s = (String) i.next();
			ps.print(s + "\n");
		}
	}

	protected void doTag(FlairMessage msg, String pseudoURI, String tags) throws Exception {
		Resource object = msg.getProfile().getSchemaModel()
			.pseudoURIToResource(pseudoURI);

		ITagModel tagModel = (ITagModel)
			msg.getProfile().getStructuredModel(ITagModel.class);

		tagModel.clearTags(object);
		if (tags != null) {
			tagModel.assignTags(object, tags.split(","));
		}
	}

	protected void doTagAll(FlairMessage msg, String tags) throws Exception {
		Profile profile = msg.getProfile();

		Query query = msg.m_query;
		IQueryModel queryModel = (IQueryModel)
			profile.getStructuredModel(IQueryModel.class);

		Set objects = queryModel.queryObjects(query, false);

		ITagModel tagModel = (ITagModel)
			msg.getProfile().getStructuredModel(ITagModel.class);

		//tagModel.clearTags(objects);
		if (tags != null) {
			tagModel.assignTags(objects, tags.split(","));
		}
	}
}
